package com.study.zzedu.ui.evaluate;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;

import com.study.zzedu.R;
import com.study.zzedu.adapter.EvaluateAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.entity.EvaluateItemModel;
import com.study.zzedu.ui.modifypwd.ModifyPwdActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.LoadingDialog;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class EvaluateActivity extends BaseMvpActivity<EvaluateContract.IView,EvaluatePresenter> implements EvaluateContract.IView{

    private EvaluateAdapter adapter;
    private List<EvaluateItemModel> eList = new ArrayList<>();
    private RecyclerView rView;
    private RelativeLayout btnBack;
    private List idList = new ArrayList();
    @Override
    protected int getLayoutID() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected EvaluatePresenter createPresenter() {
        return new EvaluatePresenter();
    }

    @Override
    protected EvaluateContract.IView createView() {
        return this;
    }
    LinearLayoutManager layoutManager;
    @Override
    protected void initView() {

        btnBack = findViewById(R.id.rl_back);
        rView = (RecyclerView)findViewById(R.id.rv_evaluate);
        layoutManager =  new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return true;
            }
        };
        rView.setLayoutManager(layoutManager);
    }

    @Override
    protected void initData() {
        getPresenter().checkUserCourseAssess(EvaluateActivity.this, AppConstant.courseCode);
    }

    @Override
    protected void initListener() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    public void submit(Context context, String userCode, String courseCode, String subjectCodes, String scores, String content){
        autoProgress(true);
        getPresenter().submitData(context,userCode,courseCode,subjectCodes,scores,content);
    }
    @Override
    public void getEvaluateListData(String data) {
        eList.clear();
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                //大title
                String title = jsonArray.getJSONObject(i).getString("title");
                JSONArray pArray = jsonArray.getJSONObject(i).getJSONArray("children");
                for(int p=0;p<pArray.length();p++){
                    //小title
                    String title1 = pArray.getJSONObject(p).getString("title");
                    Double score = pArray.getJSONObject(p).getDouble("score");
                    int id = pArray.getJSONObject(p).getInt("id");
                    boolean isFlag = p == 0?true:false;
                    idList.add(id);
                    eList.add(new EvaluateItemModel(isFlag,title,title1,score,id));
                }
            }

            if (!(jsonArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }else {
                adapter = new EvaluateAdapter(eList,EvaluateActivity.this,idList);
                rView.addItemDecoration(new SpacesItemDecoration(30));
                rView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            LogUtil.e("Exception", e.getMessage());
        }
    }

    @Override
    public void getSubmitBack(String name) {
        autoProgress(false);
        ToastUtil.showShortToast("信息已提交，感谢！");
    }

    @Override
    public void checkUserCourseAssessBack(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");
            if(jsonArray != null && jsonArray.length()>0){
                String result = jsonArray.get(0).toString();
                if("0".equals(result)){

                    AlertDialog.Builder build = new AlertDialog.Builder(EvaluateActivity.this);
                    build.setTitle("提示").setCancelable(false).setMessage("不满足评价条件！").setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO Auto-generated method stub
                                    EvaluateActivity.this.finish();
                                }
                            }).show();
                }else {
                    getPresenter().getEvaluateInfoList(EvaluateActivity.this);
                }
            }
        } catch (Exception e) {
            LogUtil.e("Exception", e.getMessage());
        }
    }

    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        autoProgress(false);
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    private LoadingDialog mDialog;
    private void autoProgress(boolean show){
        if(mDialog==null){
            mDialog = new LoadingDialog(EvaluateActivity.this);
            //mDialog.setMessage("正在加载数据。。。");
        }
        if(show){
            mDialog.show();
        }else{
            mDialog.dismiss();
        }
    }
}